// components/heart/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    stature:{
      type:Number,
    },
    weight:{
      type:Number,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    BMInum:0,//bmR值
    userweight:'',//用户体重
    userheith:'',//用户身高
    userage:'',//年龄
    sexflag:0,//性别
    sexList:[{name:'男性',value:'0',checked:true,},{name:'女性',value:'1',}],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    inputheith(e){
      if((/^[0-9]+$/).test(e.detail.value)){
        this.setData({
          userheith:e.detail.value
        })
        return        
      }
      this.setData({
        userheith:''
      })
    },
    inputwidth(e){
        if((/^[0-9]+$/).test(e.detail.value)){
          this.setData({
            userweight:e.detail.value
          })
          return        
        }
        this.setData({
          userweight:''
        })
    },
    inputwidth2(e){
      if((/^[0-9]+$/).test(e.detail.value)){
        this.setData({
          userage:e.detail.value
        })
        return        
      }
      this.setData({
        userage:''
      })
  },
    sexrChange(e){
      const {sexList} = this.data;
      let num='0';
      for (let i = 0, len = sexList.length; i < len; ++i) {
        sexList[i].checked=sexList[i].value === e.detail.value
        num=e.detail.value
      }
      this.setData({
        sexList,
        sexflag:num
      })
    },

    click(){
      // <!-- 男性: BMR = 66+(13.7× 体重(kg))+(5×身高(cm)) – (6.8×年龄(岁)) -->
// <!-- 女性: BMR = 655+(9.6×体重(kg))+(1.8×身高(cm))–(4.7×年龄(岁)) -->
      const {sexflag,userheith,userweight,userage} = this.data
      if(userheith&&userweight&&userage){
       let BMR=China({sexflag,userheith,userweight,userage})
       this.setData({
         BMInum:BMR,
       })
     this.triggerEvent('getquantity',{BMInum:this.data.BMInum})
    }else{
        wx.showToast({
          title: '请输入可靠数据！',
          icon:'error',
          duration: 2000,
          mask:true
        })
      }
    },
  }
})


function China(obj) {
  let {sexflag,userheith,userweight,userage}=obj;
    let BMR=null
// <!-- 男性: BMR = 66+(13.7× 体重(kg))+(5×身高(cm)) – (6.8×年龄(岁)) -->
// <!-- 女性: BMR = 655+(9.6×体重(kg))+(1.8×身高(cm))–(4.7×年龄(岁)) -->
      if(sexflag){
       BMR= 66+(13.7*userweight)+(5*userheith) - (6.8*userage)
      }else{
        BMR= 655+(9.6*userweight)+(1.8*userheith) - (4.7*userage)
      }
      return BMR  
}